package kr.saeildamil.androidStudy_second.fragment;

import kr.saeildamil.androidStudy_second.R;
import kr.saeildamil.androidStudy_second.fragment.common.CounterFragment01;
import kr.saeildamil.androidStudy_second.fragment.common.CounterFragment02;
import kr.saeildamil.androidStudy_second.fragment.common.TextFragment;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class FragmentManagerTest extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch20_fragmentmanagertest);
	}

	public void mOnClick(View v) {
		FragmentManager fm = getFragmentManager();
		Fragment fragment = fm.findFragmentById(R.id.ch20_fragmgntest_frame);

		switch (v.getId()) {
		case R.id.ch20_fragmgntest_addbtn:
			if (fragment == null) {
				FragmentTransaction tr = fm.beginTransaction();
				CounterFragment01 cf = new CounterFragment01();
				tr.add(R.id.ch20_fragmgntest_frame, cf, "counter");
				tr.commit();

				// fm.beginTransaction().add(R.id.ch20_fragmgntest_frame, cf,
				// "counter").commit();
			} else {
				Toast.makeText(this, "이미 프래그먼트가 존재합니다.", Toast.LENGTH_LONG)
						.show();
			}
			break;
		case R.id.ch20_fragmgntest_removebtn:
			if (fragment == null) {
				Toast.makeText(this, "삭제할 프래그먼트가 없음", Toast.LENGTH_LONG).show();
			} else {
				FragmentTransaction tr = fm.beginTransaction();
				tr.remove(fragment);
				tr.commit();
			}
			break;
		case R.id.ch20_fragmgntest_replacebtn:
			if (fragment == null) {
				Toast.makeText(this, "교체할 프래그먼트가 없음", Toast.LENGTH_LONG).show();
			} else {
				if (fragment.getTag().equals("counter")) {
					fm.beginTransaction()
							.replace(R.id.ch20_fragmgntest_frame,
									new TextFragment(), "text").commit();
				} else {
					fm.beginTransaction()
							.replace(R.id.ch20_fragmgntest_frame,
									new CounterFragment02(), "counter")
							.commit();
				}
			}
			break;
		case R.id.ch20_fragmgntest_hidebtn:
			if (fragment == null) {
				Toast.makeText(this, "프래그먼트가 없음", Toast.LENGTH_LONG).show();
			} else {
				if (fragment.isHidden()) {
					fm.beginTransaction().show(fragment).commit();
				} else {
					fm.beginTransaction().hide(fragment).commit();
				}
			}
			break;
		}
	}

}
